package com.youaishujuhui.devops.web.controller.game;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nutz.dao.Cnd;
import org.nutz.dao.FieldFilter;
import org.nutz.dao.util.Daos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.youaishujuhui.devops.web.domain.game.Game;
import com.youaishujuhui.devops.web.po.GamePO;
import com.youaishujuhui.devops.web.service.channel.ChannelService;
import com.youaishujuhui.devops.web.service.game.GameProcessService;
import com.youaishujuhui.devops.web.service.game.GameService;
import com.youaishujuhui.devops.web.util.syslog.SystemLog;
import com.youaishujuhui.devops.web.vo.Page;
import com.youaishujuhui.devops.web.vo.Result;

@Controller
@RequestMapping("/game/game")
public class GameController {

	@Autowired
	private GameService gameService;
	
	@Autowired
	private GameProcessService gameProcessService;
	
	@Autowired
	private ChannelService channelService;
	
    @RequestMapping("/page")
    public ModelAndView page(){
        return new ModelAndView("game/game");
    }
    
    @SystemLog(module = "游戏管理", oper = "添加游戏")
    @RequestMapping("/add")
    @ResponseBody
    public Result<String> add(Game po){
        Result<String> result = new Result<>();
        
        result.setCode(Result.SUCCESS);
        result.setMsg("添加成功");
        po.setState(1);
        gameService._insert(po);

        return result;
    }
    
    @RequestMapping("/findById")
    @ResponseBody
    public Result<Game> findById(Integer id){
        Result<Game> result = new Result<>();
        Game po = gameService.fetchExistGame(id);
        if(po == null){
            result.setCode(Result.FAIL);
            result.setMsg(String.format("记录已不存在！"));
        } else {
        	po = gameService._fetchLinks(po, null);
            result.setCode(Result.SUCCESS);
            result.setData(po);
        }

        return result;
    }
    
    @SystemLog(module = "游戏管理", oper = "修改游戏")
    @RequestMapping("/edit")
    @ResponseBody
    public Result<String> edit(Game po){
        Result<String> result = new Result<>();
        
        Game oldGame = gameService.fetchExistGame(po.getGameId());
        
        int existProcessCnt = gameProcessService.count(Cnd.where("gameId", "=", po.getGameId()).
        		and("state", "=", 1));
        
        if (existProcessCnt > 0 && (!oldGame.getNameEn().equals(po.getNameEn()))) {
        	result.setCode(Result.FAIL);
            result.setMsg("此游戏已开服，不允许修改游戏英文名");
            return result;
        }
        
        if (existProcessCnt > 0 && (!oldGame.getGameDbName().equals(po.getGameDbName()))) {
        	result.setCode(Result.FAIL);
            result.setMsg("此游戏已开服，不允许修改逻辑数据库名");
            return result;
        }
        
        if (existProcessCnt > 0 && (!oldGame.getLogDbName().equals(po.getLogDbName()))) {
        	result.setCode(Result.FAIL);
            result.setMsg("此游戏已开服，不允许修改日志数据库名");
            return result;
        }
        
        if (existProcessCnt > 0 && (!oldGame.getPackageDir().equals(po.getPackageDir()))) {
        	result.setCode(Result.FAIL);
            result.setMsg("此游戏已开服，不允许修改服务端程序包目录名");
            return result;
        }
        
        if (existProcessCnt > 0 && (!oldGame.getDomain().equals(po.getDomain()))) {
        	result.setCode(Result.FAIL);
            result.setMsg("此游戏已开服，不允许修改域名");
            return result;
        }
        
        // 不更新state字段
        FieldFilter fieldFilter = FieldFilter.locked(Game.class, "^state$");
        Daos.ext(gameService.dao(), fieldFilter).update(po);
        
        result.setCode(Result.SUCCESS);
        result.setMsg("修改成功！");
        return result;
    }
    
    @SystemLog(module = "游戏管理", oper = "删除游戏")
    @RequestMapping("/del")
    @ResponseBody
    public Result<String> delete(Integer id){
        Result<String> result = new Result<>();
        
        if (channelService.count(Cnd.where("gameId", "=", id)) > 0) {
        	result.setCode(Result.FAIL);
            result.setMsg("还有渠道关联此游戏，不允许删除");
            return result;
        }
        
        if (gameProcessService.count(Cnd.where("gameId", "=", id).
        		and("state", "=", 1)) > 0) {
        	result.setCode(Result.FAIL);
            result.setMsg("此游戏还有游戏服运行，不允许删除");
            return result;
        }
        
        // 只更新state字段
        Game po = new Game();
        po.setGameId(id);
        po.setState(0);
        FieldFilter fieldFilter = FieldFilter.create(Game.class, "^state$");
        Daos.ext(gameService.dao(), fieldFilter).update(po);
        
        result.setCode(Result.SUCCESS);
        result.setMsg("删除成功！");
        return result;
    }
	
    @RequestMapping("/query")
    @ResponseBody
    public Result<List<Game>> query(Integer limit,Integer offset,String sort,String order ){
        Map<String,Object> param = new HashMap<>();
        param.put("limit",limit);
        param.put("offset",offset);
        param.put("sort",sort);
        param.put("order",order);
        Result<List<Game>> result = new Result<>();
        int total = gameService.count(Cnd.where("state", "=", 1));

        List<Game> list = gameService.query(param);
        Page page = new Page(total,limit);
        result.setPage(page);
        result.setData(list);
        result.setCode(Result.SUCCESS);

        //System.out.println(JSON.toJSONString(result));

        return result;
    }
    
    @RequestMapping("/queryAll")
    @ResponseBody
    public Result<List<GamePO>> query(){
        Result<List<GamePO>> result = new Result<>();

        List<GamePO> list = gameService.queryAllGamePO();

        result.setData(list);
        result.setCode(Result.SUCCESS);

        //System.out.println(JSON.toJSONString(result));

        return result;
    }
}
